﻿using SM.Media;
using SM.Media.Utility;
using SM.Media.Web;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace BBS
{
    public sealed partial class StreamPlayer : UserControl
    {
        public event RoutedEventHandler CurrentStateChanged;

        IMediaStreamFascade _mediaStreamFascade;
        readonly IMediaElementManager _mediaElementManager;
        static readonly IApplicationInformation ApplicationInformation;
        readonly IHttpClients _httpClients;

        static StreamPlayer()
        {
            ApplicationInformation = ApplicationInformationFactory.DefaultTask.Result;
        }

        public StreamPlayer()
        {
            this.InitializeComponent();

            if (DesignMode.DesignModeEnabled == true)
            {
                return;
            }

            _mediaElementManager = new WinRtMediaElementManager(Dispatcher,
               () =>
               {
                   return mediaElement;
               }, me => { });

            var userAgent = ApplicationInformation.CreateUserAgent();

            _httpClients = new HttpClients(userAgent: userAgent);

            Unloaded += (sender, args) => OnUnload();

            InitializeMediaStream();
        }

        void OnMediaCurrentStateChanged(RoutedEventArgs e)
        {
            if (CurrentStateChanged == null)
            {
                return;
            }

            CurrentStateChanged(this.mediaElement, e);
        }

        public void OnUnload()
        {
            if (null != mediaElement)
            {
                mediaElement.Source = null;
            }

            var mediaStreamFascade = _mediaStreamFascade;
            _mediaStreamFascade = null;

            mediaStreamFascade.DisposeBackground("MainPage unload");
        }

        void InitializeMediaStream()
        {
            if (null != _mediaStreamFascade)
            {
                return;
            }

            _mediaStreamFascade = MediaStreamFascadeSettings.Parameters.Create(_httpClients, _mediaElementManager.SetSourceAsync);
            _mediaStreamFascade.SetParameter(_mediaElementManager);
            this.mediaElement.CurrentStateChanged += mediaElement_CurrentStateChanged;
        }

        void mediaElement_CurrentStateChanged(object sender, RoutedEventArgs e)
        {
            OnMediaCurrentStateChanged(e);
        }

        public void SetSource(string url)
        {
            _mediaStreamFascade.Source = new Uri(url);
        }

        public void Play()
        {
            this.mediaElement.Play();
        }

        public void Stop()
        {
            this.mediaElement.Stop();
            this.mediaElement.Source = null;
        }

        public void Pause()
        {
            this.mediaElement.Pause();
        }

        public MediaElementState CurrentState
        {
            get 
            { 
                if (this.mediaElement == null)
                {
                    return MediaElementState.Closed;
                }

                return this.mediaElement.CurrentState; 
            }
        }
    }
}
